package com.agan.workflow.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.agan.common.core.utils.StringUtils;
import com.agan.common.core.web.domain.ResponseResult;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.agan.common.log.annotation.Log;
import com.agan.common.log.enums.BusinessType;
import com.agan.common.security.annotation.RequiresPermissions;
import com.agan.workflow.domain.BpmType;
import com.agan.workflow.service.IBpmTypeService;
import com.agan.common.core.web.controller.BaseController;
import com.agan.common.core.utils.poi.ExcelUtil;

/**
 * 流程分类Controller
 * 
 * @author agan
 * @date 2024-01-10
 */
@RestController
@RequestMapping("/type")
@RequiredArgsConstructor
public class BpmTypeController extends BaseController
{
    private final IBpmTypeService bpmTypeService;

    /**
     * 查询流程分类列表
     */
    @RequiresPermissions("bpm:type:list")
    @GetMapping("/list")
    public ResponseResult list(BpmType bpmType)
    {
        List<BpmType> list = bpmTypeService.selectBpmTypeList(bpmType);
        return success(list);
    }

    /**
     * 查询流程分类列表（排除节点）
     */
    @RequiresPermissions("bpm:dept:list")
    @GetMapping("/list/exclude/{typeId}")
    public ResponseResult excludeChild(@PathVariable(value = "typeId", required = false) Long typeId)
    {
        List<BpmType> types = bpmTypeService.selectBpmTypeList(new BpmType());
        types.removeIf(d -> d.getTypeId().intValue() == typeId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), typeId + ""));
        return success(types);
    }

    /**
     * 导出流程分类列表
     */
    @RequiresPermissions("bpm:type:export")
    @Log(title = "流程分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BpmType bpmType)
    {
        List<BpmType> list = bpmTypeService.selectBpmTypeList(bpmType);
        ExcelUtil<BpmType> util = new ExcelUtil<BpmType>(BpmType.class);
        util.exportExcel(response, list, "流程分类数据");
    }

    /**
     * 获取流程分类详细信息
     */
    @RequiresPermissions("bpm:type:query")
    @GetMapping(value = "/{typeId}")
    public ResponseResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(bpmTypeService.selectBpmTypeByTypeId(typeId));
    }

    /**
     * 新增流程分类
     */
    @RequiresPermissions("bpm:type:add")
    @Log(title = "流程分类", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseResult add(@RequestBody BpmType bpmType)
    {
        return toResponse(bpmTypeService.insertBpmType(bpmType));
    }

    /**
     * 修改流程分类
     */
    @RequiresPermissions("bpm:type:edit")
    @Log(title = "流程分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseResult edit(@RequestBody BpmType bpmType)
    {
        return toResponse(bpmTypeService.updateBpmType(bpmType));
    }

    /**
     * 删除流程分类
     */
    @RequiresPermissions("bpm:type:remove")
    @Log(title = "流程分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public ResponseResult remove(@PathVariable Long[] typeIds)
    {
        return toResponse(bpmTypeService.deleteBpmTypeByTypeIds(typeIds));
    }
}
